home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / sendmail-5.65c+IDA-1.4.4.1 / src / RCS / vprintf.c,v < prev    next >
Encoding:
Text File  |  1991-06-25  |  13.0 KB  |  687 lines

  1. head    1.3;
  2. access;
  3. symbols
  4.     RELEASE:1.3
  5.     BETA:1.2;
  6. locks; strict;
  7. comment    @ * @;
  8.  
  9.  
  10. 1.3
  11. date    91.06.24.14.12.03;    author paul;    state Exp;
  12. branches;
  13. next    1.2;
  14.  
  15. 1.2
  16. date    91.05.22.04.08.41;    author paul;    state Exp;
  17. branches;
  18. next    1.1;
  19.  
  20. 1.1
  21. date    91.05.22.02.33.43;    author paul;    state Exp;
  22. branches;
  23. next    ;
  24.  
  25.  
  26. desc
  27. @Portable vprintf()/vfprintf()/vsprintf() from Robert A. Larson
  28. (blarson@@skat.usc.edu).  
  29. @
  30.  
  31.  
  32. 1.3
  33. log
  34. @Further ANSI changes from Bruce Lilly.
  35. @
  36. text
  37. @/*
  38.  * Portable vfprintf  by Robert A. Larson <blarson@@skat.usc.edu>
  39.  *
  40.  * Copyright 1989 Robert A. Larson.
  41.  * Distribution in any form is allowed as long as the author
  42.  * retains credit, changes are noted by their author and the
  43.  * copyright message remains intact.  This program comes as-is
  44.  * with no warentee of fitness for any purpouse.
  45.  *
  46.  * Thanks to Doug Gwen, Chris Torek, and others who helped clarify
  47.  * the ansi printf specs.
  48.  *
  49.  * Please send any bug fixes and improvments to blarson@@skat.usc.edu .
  50.  * The use of goto is NOT a bug.
  51.  *
  52.  * Feb    7, 1989        blarson        First usenet release
  53.  *
  54.  * Changes for use with the IDA sendmail made by Paul Pomes, University
  55.  * of Illinois, Computing Services Office.  The original version is checked
  56.  * in to the RCS directory as number 1.1.
  57.  */
  58.  
  59. /* This code implements the vsprintf function, without relying on
  60.  * the existance of _doprint or other system specific code.
  61.  *
  62.  * Define NOVOID if void * is not a supported type.
  63.  *
  64.  * Two compile options are available for efficency:
  65.  *    INTSPRINTF    should be defined if sprintf is int and returns
  66.  *            the number of chacters formated.
  67.  *    LONGINT        should be defined if sizeof(long) == sizeof(int)
  68.  *
  69.  *    They only make the code smaller and faster, they need not be
  70.  *    defined.
  71.  *
  72.  * UNSIGNEDSPECIAL should be defined if unsigned is treated differently
  73.  * than int in argument passing.  If this is definded, and LONGINT is not,
  74.  * the compiler must support the type unsingned long.
  75.  *
  76.  * Most quirks and bugs of the available sprintf fuction are duplicated,
  77.  * however * in the width and precision fields will work correctly
  78.  * even if sprintf does not support this, as will the n format.
  79.  *
  80.  * Bad format strings, or those with very long width and precision
  81.  * fields (including expanded * fields) will cause undesired results.
  82.  */
  83. #include "sendmail.h"
  84.  
  85. #ifdef NEED_VSPRINTF
  86.  
  87. # if !defined(lint)
  88. static char sccsid[] = "@@(#)vprint.c    1.1 (USC) 2/7/89";
  89. static char rcsid[] = "@@(#)$Id: vprintf.c,v 1.2 1991/05/22 04:08:41 paul Exp $";
  90. # endif /* not lint */
  91.  
  92. # ifndef __STDC__
  93. #  define NOVOID            /* a possibly reasonable assumption */
  94. # endif /* !__STDC__ */
  95.  
  96. # ifdef OSK            /* os9/68k can take advantage of both */
  97. #  define LONGINT
  98. #  define INTSPRINTF
  99. # endif /* OSK */
  100.  
  101. /* This must be a typedef not a #define! */
  102. # ifdef NOVOID
  103. typedef char *pointer;
  104. # else /* !NOVOID */
  105. typedef void *pointer;
  106. # endif /* NOVOID */
  107.  
  108. # ifdef INTSPRINTF
  109. #  define Sprintf(string,format,arg)    (sprintf((string),(format),(arg)))
  110. # else /* !INTSPRINTF */
  111. # define Sprintf(string,format,arg)    (\
  112.     sprintf((string),(format),(arg)),\
  113.     strlen(string)\
  114. )
  115. # endif /* INTSPRINTF */
  116.  
  117. typedef int *intp;
  118.  
  119. int
  120. # ifdef __STDC__
  121. vsprintf (char *dest, const char *format, va_list args)
  122. # else /* !__STDC__ */
  123. vsprintf (dest, format, args)
  124.     char    *dest;
  125.     register char *format;
  126.     va_list  args;
  127. # endif /* __STDC__ */
  128. {
  129.     register char *dp = dest;
  130.     register char c;
  131.     register char *tp;
  132.     char     tempfmt[64];
  133.  
  134. # ifndef LONGINT
  135.     int      longflag;
  136.  
  137. # endif /* !LONGINT */
  138.  
  139.     tempfmt[0] = '%';
  140.     while (c = *format++) {
  141.         if (c == '%') {
  142.             tp = &tempfmt[1];
  143. # ifndef LONGINT
  144.             longflag = 0;
  145. # endif /* !LONGINT */
  146.     continue_format:
  147.             switch (c = *format++) {
  148.                 case 's':
  149.                 *tp++ = c;
  150.                 *tp = '\0';
  151.                 dp += Sprintf (dp, tempfmt, va_arg (args, char *));
  152.                 break;
  153.                 case 'u':
  154.                 case 'x':
  155.                 case 'o':
  156.                 case 'X':
  157. # ifdef UNSIGNEDSPECIAL
  158.                 *tp++ = c;
  159.                 *tp = '\0';
  160. #  ifndef LONGINT
  161.                 if (longflag)
  162.                     dp += Sprintf (dp, tempfmt, va_arg (args, unsigned long));
  163.                 else
  164. #  endif /* !LONGINT */
  165.                     dp += Sprintf (dp, tempfmt, va_arg (args, unsigned));
  166.                 break;
  167. # endif /* UNSIGNEDSPECIAL */
  168.                 case 'd':
  169.                 case 'c':
  170.                 case 'i':
  171.                 *tp++ = c;
  172.                 *tp = '\0';
  173. # ifndef LONGINT
  174.                 if (longflag)
  175.                     dp += Sprintf (dp, tempfmt, va_arg (args, long));
  176.                 else
  177. # endif /* !LONGINT */
  178.                     dp += Sprintf (dp, tempfmt, va_arg (args, int));
  179.                 break;
  180.                 case 'f':
  181.                 case 'e':
  182.                 case 'E':
  183.                 case 'g':
  184.                 case 'G':
  185.                 *tp++ = c;
  186.                 *tp = '\0';
  187.                 dp += Sprintf (dp, tempfmt, va_arg (args, double));
  188.                 break;
  189.                 case 'p':
  190.                 *tp++ = c;
  191.                 *tp = '\0';
  192.                 dp += Sprintf (dp, tempfmt, va_arg (args, pointer));
  193.                 break;
  194.                 case '-':
  195.                 case '+':
  196.                 case '0':
  197.                 case '1':
  198.                 case '2':
  199.                 case '3':
  200.                 case '4':
  201.                 case '5':
  202.                 case '6':
  203.                 case '7':
  204.                 case '8':
  205.                 case '9':
  206.                 case '.':
  207.                 case ' ':
  208.                 case '#':
  209.                 case 'h':
  210.                 *tp++ = c;
  211.                 goto continue_format;
  212.                 case 'l':
  213. # ifndef LONGINT
  214.                 longflag = 1;
  215.                 *tp++ = c;
  216. # endif /* !LONGINT */
  217.                 goto continue_format;
  218.                 case '*':
  219.                 tp += Sprintf (tp, "%d", va_arg (args, int));
  220.                 goto continue_format;
  221.                 case 'n':
  222.                 *va_arg (args, intp) = dp - dest;
  223.                 break;
  224.                 case '%':
  225.                 default:
  226.                 *dp++ = c;
  227.                 break;
  228.             }
  229.         } else
  230.             *dp++ = c;
  231.     }
  232.     *dp = '\0';
  233.     return dp - dest;
  234. }
  235.  
  236.  
  237. int 
  238. # ifdef __STDC__
  239. vfprintf (FILE *dest, const char *format, va_list args)
  240. # else /* !__STDC__ */
  241. vfprintf (dest, format, args)
  242.     FILE    *dest;
  243.     register char *format;
  244.     va_list  args;
  245. # endif /* __STDC__ */
  246. {
  247.     register char c;
  248.     register char *tp;
  249.     register int count = 0;
  250.     char     tempfmt[64];
  251.  
  252. # ifndef LONGINT
  253.     int      longflag;
  254.  
  255. # endif /* !LONGINT */
  256.  
  257.     tempfmt[0] = '%';
  258.     while (c = *format++) {
  259.         if (c == '%') {
  260.             tp = &tempfmt[1];
  261. # ifndef LONGINT
  262.             longflag = 0;
  263. # endif /* !LONGINT */
  264.     continue_format:
  265.             switch (c = *format++) {
  266.                 case 's':
  267.                 *tp++ = c;
  268.                 *tp = '\0';
  269.                 count += fprintf (dest, tempfmt, va_arg (args, char *));
  270.                 break;
  271.                 case 'u':
  272.                 case 'x':
  273.                 case 'o':
  274.                 case 'X':
  275. # ifdef UNSIGNEDSPECIAL
  276.                 *tp++ = c;
  277.                 *tp = '\0';
  278. #  ifndef LONGINT
  279.                 if (longflag)
  280.                     count += fprintf (dest, tempfmt, va_arg (args, unsigned long));
  281.                 else
  282. #  endif /* !LONGINT */
  283.                     count += fprintf (dest, tempfmt, va_arg (args, unsigned));
  284.                 break;
  285. # endif /* UNSIGNEDSPECIAL */
  286.                 case 'd':
  287.                 case 'c':
  288.                 case 'i':
  289.                 *tp++ = c;
  290.                 *tp = '\0';
  291. # ifndef LONGINT
  292.                 if (longflag)
  293.                     count += fprintf (dest, tempfmt, va_arg (args, long));
  294.                 else
  295. # endif /* !LONGINT */
  296.                     count += fprintf (dest, tempfmt, va_arg (args, int));
  297.                 break;
  298.                 case 'f':
  299.                 case 'e':
  300.                 case 'E':
  301.                 case 'g':
  302.                 case 'G':
  303.                 *tp++ = c;
  304.                 *tp = '\0';
  305.                 count += fprintf (dest, tempfmt, va_arg (args, double));
  306.                 break;
  307.                 case 'p':
  308.                 *tp++ = c;
  309.                 *tp = '\0';
  310.                 count += fprintf (dest, tempfmt, va_arg (args, pointer));
  311.                 break;
  312.                 case '-':
  313.                 case '+':
  314.                 case '0':
  315.                 case '1':
  316.                 case '2':
  317.                 case '3':
  318.                 case '4':
  319.                 case '5':
  320.                 case '6':
  321.                 case '7':
  322.                 case '8':
  323.                 case '9':
  324.                 case '.':
  325.                 case ' ':
  326.                 case '#':
  327.                 case 'h':
  328.                 *tp++ = c;
  329.                 goto continue_format;
  330.                 case 'l':
  331. # ifndef LONGINT
  332.                 longflag = 1;
  333.                 *tp++ = c;
  334. # endif /* !LONGINT */
  335.                 goto continue_format;
  336.                 case '*':
  337.                 tp += Sprintf (tp, "%d", va_arg (args, int));
  338.                 goto continue_format;
  339.                 case 'n':
  340.                 *va_arg (args, intp) = count;
  341.                 break;
  342.                 case '%':
  343.                 default:
  344.                 putc (c, dest);
  345.                 count++;
  346.                 break;
  347.             }
  348.         } else {
  349.             putc (c, dest);
  350.             count++;
  351.         }
  352.     }
  353.     return count;
  354. }
  355.  
  356. int
  357. # ifdef __STDC__
  358. vprintf (const char *format, va_list args)
  359. # else /* !__STDC__ */
  360. vprintf (format, args)
  361.     register char *format;
  362.     va_list  args;
  363. # endif /* __STDC__ */
  364. {
  365.     register int nchars;
  366.     nchars = vfprintf (stdout, format, args);
  367.     return nchars;
  368. }
  369.  
  370. #endif /* NEED_VSPRINTF */
  371. @
  372.  
  373.  
  374. 1.2
  375. log
  376. @First adaptation of the vprintf package for sendmail.
  377. @
  378. text
  379. @d49 6
  380. a54 4
  381. #if !defined(lint)
  382. static char sccsid[] = "@@(#)vprint.c    1.1 (USC) 2/7/89    %I% local";
  383. static char rcsid[] = "@@(#)$Id$";
  384. #endif /* not lint */
  385. a55 1
  386. #ifdef NEED_VSPRINTF
  387. d73 2
  388. a74 2
  389. # define Sprintf(string,format,arg)    (sprintf((string),(format),(arg)))
  390. # else
  391. d84 3
  392. d91 1
  393. d202 3
  394. d209 1
  395. d320 4
  396. d325 1
  397. a325 1
  398.     char    *format;
  399. d327 1
  400. d329 3
  401. a331 1
  402.     return vfprintf (stdout, format, args);
  403. @
  404.  
  405.  
  406. 1.1
  407. log
  408. @Initial revision
  409. @
  410. text
  411. @d1 4
  412. a4 5
  413. #include <stdio.h>
  414.  
  415. /* Portable vfprintf  by Robert A. Larson <blarson@@skat.usc.edu> */
  416.  
  417. /* Copyright 1989 Robert A. Larson.
  418. d15 6
  419. a22 2
  420. /* Feb    7, 1989        blarson        First usenet release */
  421.  
  422. d47 1
  423. d49 14
  424. a62 4
  425. #ifdef OSK        /* os9/68k can take advantage of both */
  426. #define LONGINT
  427. #define INTSPRINTF
  428. #endif
  429. d65 1
  430. a65 1
  431. #ifdef NOVOID
  432. d67 1
  433. a67 1
  434. #else
  435. d69 1
  436. a69 1
  437. #endif
  438. d71 4
  439. a74 4
  440. #ifdef    INTSPRINTF
  441. #define Sprintf(string,format,arg)    (sprintf((string),(format),(arg)))
  442. #else
  443. #define Sprintf(string,format,arg)    (\
  444. d78 1
  445. a78 7
  446. #endif
  447.  
  448. #if defined(__STDC__) && !defined(NOSTDHDRS)
  449. #include <stdarg.h>
  450. #else
  451. #include <varargs.h>
  452. #endif
  453. d82 5
  454. a86 4
  455. int vsprintf(dest, format, args)
  456. char *dest;
  457. register char *format;
  458. va_list args;
  459. d88 105
  460. a192 102
  461.     register char *dp = dest;
  462.     register char c;
  463.     register char *tp;
  464.     char tempfmt[64];
  465. #ifndef LONGINT
  466.     int longflag;
  467. #endif
  468.  
  469.     tempfmt[0] = '%';
  470.     while(c = *format++) {
  471.     if(c=='%') {
  472.         tp = &tempfmt[1];
  473. #ifndef LONGINT
  474.         longflag = 0;
  475. #endif
  476. continue_format:
  477.         switch(c = *format++) {
  478.         case 's':
  479.             *tp++ = c;
  480.             *tp = '\0';
  481.             dp += Sprintf(dp, tempfmt, va_arg(args, char *));
  482.             break;
  483.         case 'u':
  484.         case 'x':
  485.         case 'o':
  486.         case 'X':
  487. #ifdef UNSIGNEDSPECIAL
  488.             *tp++ = c;
  489.             *tp = '\0';
  490. #ifndef LONGINT
  491.             if(longflag)
  492.             dp += Sprintf(dp, tempfmt, va_arg(args, unsigned long));
  493.             else
  494. #endif
  495.             dp += Sprintf(dp, tempfmt, va_arg(args, unsigned));
  496.             break;
  497. #endif
  498.         case 'd':
  499.         case 'c':
  500.         case 'i':
  501.             *tp++ = c;
  502.             *tp = '\0';
  503. #ifndef LONGINT
  504.             if(longflag)
  505.             dp += Sprintf(dp, tempfmt, va_arg(args, long));
  506.             else
  507. #endif
  508.             dp += Sprintf(dp, tempfmt, va_arg(args, int));
  509.             break;
  510.         case 'f':
  511.         case 'e':
  512.         case 'E':
  513.         case 'g':
  514.         case 'G':
  515.             *tp++ = c;
  516.             *tp = '\0';
  517.             dp += Sprintf(dp, tempfmt, va_arg(args, double));
  518.             break;
  519.         case 'p':
  520.             *tp++ = c;
  521.             *tp = '\0';
  522.             dp += Sprintf(dp, tempfmt, va_arg(args, pointer));
  523.             break;
  524.         case '-':
  525.         case '+':
  526.         case '0':
  527.         case '1':
  528.         case '2':
  529.         case '3':
  530.         case '4':
  531.         case '5':
  532.         case '6':
  533.         case '7':
  534.         case '8':
  535.         case '9':
  536.         case '.':
  537.         case ' ':
  538.         case '#':
  539.         case 'h':
  540.             *tp++ = c;
  541.             goto continue_format;
  542.         case 'l':
  543. #ifndef LONGINT
  544.             longflag = 1;
  545.             *tp++ = c;
  546. #endif
  547.             goto continue_format;
  548.         case '*':
  549.             tp += Sprintf(tp, "%d", va_arg(args, int));
  550.             goto continue_format;
  551.         case 'n':
  552.             *va_arg(args, intp) = dp - dest;
  553.             break;
  554.         case '%':
  555.         default:
  556.             *dp++ = c;
  557.             break;
  558.         }
  559.     } else *dp++ = c;
  560.     }
  561.     *dp = '\0';
  562.     return dp - dest;
  563. d196 5
  564. a200 4
  565. int vfprintf(dest, format, args)
  566. FILE *dest;
  567. register char *format;
  568. va_list args;
  569. d202 105
  570. a306 102
  571.     register char c;
  572.     register char *tp;
  573.     register int count = 0;
  574.     char tempfmt[64];
  575. #ifndef LONGINT
  576.     int longflag;
  577. #endif
  578.  
  579.     tempfmt[0] = '%';
  580.     while(c = *format++) {
  581.     if(c=='%') {
  582.         tp = &tempfmt[1];
  583. #ifndef LONGINT
  584.         longflag = 0;
  585. #endif
  586. continue_format:
  587.         switch(c = *format++) {
  588.         case 's':
  589.             *tp++ = c;
  590.             *tp = '\0';
  591.             count += fprintf(dest, tempfmt, va_arg(args, char *));
  592.             break;
  593.         case 'u':
  594.         case 'x':
  595.         case 'o':
  596.         case 'X':
  597. #ifdef UNSIGNEDSPECIAL
  598.             *tp++ = c;
  599.             *tp = '\0';
  600. #ifndef LONGINT
  601.             if(longflag)
  602.             count += fprintf(dest, tempfmt, va_arg(args, unsigned long));
  603.             else
  604. #endif
  605.             count += fprintf(dest, tempfmt, va_arg(args, unsigned));
  606.             break;
  607. #endif
  608.         case 'd':
  609.         case 'c':
  610.         case 'i':
  611.             *tp++ = c;
  612.             *tp = '\0';
  613. #ifndef LONGINT
  614.             if(longflag)
  615.             count += fprintf(dest, tempfmt, va_arg(args, long));
  616.             else
  617. #endif
  618.             count += fprintf(dest, tempfmt, va_arg(args, int));
  619.             break;
  620.         case 'f':
  621.         case 'e':
  622.         case 'E':
  623.         case 'g':
  624.         case 'G':
  625.             *tp++ = c;
  626.             *tp = '\0';
  627.             count += fprintf(dest, tempfmt, va_arg(args, double));
  628.             break;
  629.         case 'p':
  630.             *tp++ = c;
  631.             *tp = '\0';
  632.             count += fprintf(dest, tempfmt, va_arg(args, pointer));
  633.             break;
  634.         case '-':
  635.         case '+':
  636.         case '0':
  637.         case '1':
  638.         case '2':
  639.         case '3':
  640.         case '4':
  641.         case '5':
  642.         case '6':
  643.         case '7':
  644.         case '8':
  645.         case '9':
  646.         case '.':
  647.         case ' ':
  648.         case '#':
  649.         case 'h':
  650.             *tp++ = c;
  651.             goto continue_format;
  652.         case 'l':
  653. #ifndef LONGINT
  654.             longflag = 1;
  655.             *tp++ = c;
  656. #endif
  657.             goto continue_format;
  658.         case '*':
  659.             tp += Sprintf(tp, "%d", va_arg(args, int));
  660.             goto continue_format;
  661.         case 'n':
  662.             *va_arg(args, intp) = count;
  663.             break;
  664.         case '%':
  665.         default:
  666.             putc(c, dest);
  667.             count++;
  668.             break;
  669.         }
  670.     } else {
  671.         putc(c, dest);
  672.         count++;
  673. d308 1
  674. a308 2
  675.     }
  676.     return count;
  677. d311 3
  678. a313 3
  679. vprintf(format, args)
  680. char *format;
  681. va_list args;
  682. d315 1
  683. a315 1
  684.     return vfprintf(stdout, format, args);
  685. d317 2
  686. @
  687.